package com.gengzp.mq.client.utils;

import com.gengzp.mq.client.core.MqServerRequestFunc;
import jakarta.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName MessageUtils
 * @Description 消息工具类
 * @Author gengzp
 * @Date 2025/6/10 0:24
 */
@Component
public class MqMessageUtils {

    private static final Logger logger = LoggerFactory.getLogger(MqMessageUtils.class);

    @Autowired
    private MqServerRequestFunc mqServerRequestFuncInstance;
    private static MqServerRequestFunc mqServerRequestFunc;

    private MqMessageUtils() {
    }

    @PostConstruct
    private void init() {
        mqServerRequestFunc = mqServerRequestFuncInstance;
    }

    /**
     * 发送消息
     *
     * @param topic          主题
     * @param messageContent 消息内容
     * @return 消息id
     */
    public static Long sendMessage(String topic, String messageContent) {
        return mqServerRequestFunc.sendMessage(topic, messageContent);
    }

    /**
     * 发送消息, 不管是否成功
     *
     * @param topic          主题
     * @param messageContent 消息内容
     */
    public static void sendMessageOneWay(String topic, String messageContent) {
        try {
            new Thread(() -> mqServerRequestFunc.sendMessage(topic, messageContent),
                    "MqMessageUtils-sendMessageOneWay");
        } catch (Exception e) {
            logger.info("发送消息失败", e);
        }
    }

}
